<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <base data-ice="baseUrl" href="../../../">
  <title data-ice="title">app/service/user.js | API Document</title>
  <link type="text/css" rel="stylesheet" href="css/style.css">
  <link type="text/css" rel="stylesheet" href="css/prettify-tomorrow.css">
  <script src="script/prettify/prettify.js"></script>
  
  
  <script src="script/manual.js"></script>
</head>
<body class="layout-container" data-ice="rootContainer">

<header>
  <a href="./">Home</a>
  
  <a href="identifiers.html">Reference</a>
  <a href="source.html">Source</a>
  
  
  <div class="search-box">
  <span>
    <img src="./image/search.png">
    <span class="search-input-edge"></span><input class="search-input"><span class="search-input-edge"></span>
  </span>
    <ul class="search-result"></ul>
  </div>
</header>

<nav class="navigation" data-ice="nav"><div>
  <ul>
    
  </ul>
</div>
</nav>

<div class="content" data-ice="content"><h1 data-ice="title">app/service/user.js</h1>
<pre class="source-code line-number raw-source-code"><code class="prettyprint linenums" data-ice="content">&apos;use strict&apos;;
const crypto = require(&apos;crypto&apos;);

module.exports = app =&gt; {
  class UserService extends app.Service {
    // &#x7528;&#x6237;&#x767B;&#x5F55;
    * login(user) {
      // &#x5BC6;&#x7801;&#x505A;md5&#x5904;&#x7406;
      user.password = crypto.createHash(&apos;md5&apos;).update(user.password).digest(&apos;hex&apos;);
      // &#x9A8C;&#x8BC1;&#x7528;&#x6237;&#x540D;&#x548C;&#x5BC6;&#x7801;
      if (yield this.checkUser(user.userId, user.password)) {
      // &#x521B;&#x5EFA;json token,&#x8BBE;&#x7F6E;7&#x5929;&#x8FC7;&#x671F;
        const token = this.ctx.service.user.createToken({ userId: user.userId });
        //&#x8BBE;&#x7F6E;&#x5934;&#x6587;&#x4EF6; http header token
        this.ctx.set(&quot;token&quot;,token);
      // &#x4FDD;&#x5B58;&#x5230;&#x7528;&#x6237;&#x6570;&#x636E;&#x5E93;
        user.token = token;
        user = yield this.findOneAndUpdate({ userId: user.userId }, user);
        return user;
      }
      return {
        isSuccess: false,
        msg: &apos;&#x7528;&#x6237;&#x540D;&#x6216;&#x5BC6;&#x7801;&#x9519;&#x8BEF;&apos;,
      };
    }
    // &#x6CE8;&#x518C;&#x7528;&#x6237;
    * register(user) {
      if (yield this.hasRegister(user.userId)) {
        return {
          isSuccess: false,
          msg: &apos;&#x7528;&#x6237;&#x5DF2;&#x5B58;&#x5728;&apos;,
        };
      }
      user.password = crypto.createHash(&apos;md5&apos;).update(user.password).digest(&apos;hex&apos;);
      const newUser = yield app.model.user.create(user);
      this.login(newUser);
      return {
        isSuccess: true,
        user: newUser,
      };
    }
    // &#x8BE5;&#x8D26;&#x53F7;&#x662F;&#x5426;&#x5DF2;&#x7ECF;&#x6CE8;&#x518C;
    * hasRegister(userId) {
      const user = yield app.model.user.findOne({ userId });
      if (user &amp;&amp; user.userId) {
        return true;
      }
      return false;
    }
    // &#x9A8C;&#x8BC1;&#x8D26;&#x53F7;&#x5BC6;&#x7801;&#x662F;&#x5426;&#x6B63;&#x786E;
    * checkUser(userId, password) {
      const user = yield app.model.user.findOne({ userId, password });
      if (user &amp;&amp; user.userId) {
        return true;
      }
      return false;
    }
    // &#x9A8C;&#x8BC1;&#x662F;&#x5426;&#x767B;&#x5F55;
    * isLogin(token) {
      return this.ctx.service.user.verifyToken(token);
    }
  }
  return UserService;
};
</code></pre>

</div>

<footer class="footer">
  Generated by <a href="https://esdoc.org">ESDoc<span data-ice="esdocVersion">(0.5.2)</span><img src="./image/esdoc-logo-mini-black.png"></a>
</footer>

<script src="script/search_index.js"></script>
<script src="script/search.js"></script>
<script src="script/pretty-print.js"></script>
<script src="script/inherited-summary.js"></script>
<script src="script/test-summary.js"></script>
<script src="script/inner-link.js"></script>
<script src="script/patch-for-local.js"></script>
</body>
</html>
